package daos;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;

import entidades.Turma;

public class DAOturma {

	public void salvar (Turma a){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		em.persist(a);
		em.getTransaction().commit();
	em.close();
	emf.close();
		
	}
	public void alterar(Turma a){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		em.merge(a) ;
		em.getTransaction().commit();
	em.close();
	emf.close();
	}
	@SuppressWarnings("unchecked")
	public List<Turma> listar(){
		EntityManagerFactory emf = Persistence.createEntityManagerFactory("escola_2");
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		Query b = em.createQuery("SELECT c FROM Turma c");
		List<Turma> lista = b.getResultList();
		em.getTransaction().commit();
		em.close();
		emf.close();
		return lista;
	}
	
}
